import Vue from 'vue'
/*
 * 点击除了绑定指令元素其他dom发生绑定的事件
 * 一般用于弹出层 点击其他地方隐藏弹出层
 *
 */
Vue.directive("click-other-place", {
   inserted: function (el, binding) {
       function clickHandler(e) {
           if (el.contains(e.target)) return;
           if (binding.expression) {
               binding.value(e);
           }
       }

       // 绑定一个变量用于解除绑定
       el._clickOtherPlace = clickHandler;
       document.addEventListener("click", clickHandler);
   },
   unbind: function (el) {
       document.removeEventListener("click", el._clickOtherPlace);
       delete el._clickOtherPlace;
   }
});